package com.example.config;

import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.alibaba.druid.pool.DruidDataSourceFactory;

@Configuration    //该注解类似于spring配置文件
@MapperScan(basePackages="com.example.mapper")
public class MyBatisConfig {

 
    /**
      * 创建数据源
      * @Primary 该注解表示在同一个接口有多个实现类可以注入的时候，默认选择哪一个，而不是让@autowire注解报错 
      */
     @Bean
     //@Primary
     public DataSource getDataSource() throws Exception{
         Properties props = new Properties();
         props.put("driverClassName", "com.mysql.jdbc.Driver");
         props.put("url", "jdbc:mysql://192.168.0.101:3306/boot?useUnicode=true&characterEncoding=UTF8");
        props.put("username", "root");
         props.put("password", "1234");
         return DruidDataSourceFactory.createDataSource(props);
     }
 
     /**
      * 根据数据源创建SqlSessionFactory
      */
     @Bean
     public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception{
         SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
         fb.setDataSource(ds);//指定数据源(这个必须有，否则报错)
         //下边两句仅仅用于*.xml文件，如果整个持久层操作不需要使用到xml文件的话（只用注解就可以搞定），则不加
         fb.setTypeAliasesPackage("com.example.po");//指定基包
         fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));//指定xml文件位置
         
         return fb.getObject();
     }

}
